package cn.daniel.goods.controller;


import cn.daniel.commonutils.Response;
import cn.daniel.goods.entity.GoodsInfo;
import cn.daniel.goods.service.GoodsInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Daniel_li
 * @since 2020-12-29
 */
@RestController
@RequestMapping("/service/goods/info")
public class GoodsInfoController {

    @Autowired
    private GoodsInfoService goodsInfoService;

    @GetMapping("/adminLogin/{username}/{pwd}")
    public Response adminLogin(@PathVariable String username,
                               @PathVariable String pwd){
        if (username.equals("admin")&&pwd.equals("123456"))
            return Response.ok();
        return Response.error();

    }
    @GetMapping("/info")
    public Response getInfo(){
        return Response.ok().data("roles","[admin]")
                .data("name","admin")
                .data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }
    /**
     * 根据ID查询goods
     * @param id
     * @return
     */
    @GetMapping("/getGoodsOne/{id}")
    public Response getGoodsOne(@PathVariable String id){
        GoodsInfo goodsInfo = goodsInfoService.getById(id);
        return Response.ok().data("goods",goodsInfo);
    }

    /**
     * 分页查询goods
     * @param page
     * @param size
     * TTOD 后续完成分页查询
     *
     * @return
     */
    @GetMapping("/getGoodsPageList/page/limit")
    public Response getGoodsPageList(@PathVariable long page,
                                     @PathVariable long size){
        Page<GoodsInfo> goodsInfoPage = new Page<>(page, size);
        //
        return Response.ok();
    }

    /**
     * 添加或修改Goods
     * @param goodsInfo
     * @return
     */
    @PostMapping("/addOrUpdateGoods")
    public Response addGoods(@RequestBody GoodsInfo goodsInfo){
        if (StringUtils.isEmpty(goodsInfo.getGoodsId())){
            boolean save = goodsInfoService.save(goodsInfo);
            String goodsId=goodsInfo.getGoodsId();
            return save ? Response.ok().message("添加成功").data("goodsId",goodsId):Response.error().message("添加失败");
        }else {
            boolean b = goodsInfoService.updateById(goodsInfo);
            return b ? Response.ok().message("修改成功").data("goodsId",goodsInfo.getGoodsId()):Response.error().message("修改失败");
        }
    }

    /**
     * 根据id删除goods
     * @param id
     * @return
     */
    @DeleteMapping("/removeGoods/{id}")
    public Response removeGoods(@PathVariable String id){
        boolean b = goodsInfoService.removeById(id);
        return b ? Response.ok().message("删除成功"):Response.error().message("删除失败");
    }



}

